 
// Source File Name:   PQImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.CV;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.PQ;
import com.sunwayhorizo.health.hl7.datatypes.QTY;
import com.sunwayhorizo.health.hl7.datatypes.REAL;
import com.sunwayhorizo.health.hl7.datatypes.SET_PQR;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.math.BigDecimal;


// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, CSImpl, NullFlavorImpl, 
//            REALImpl, STImpl, TYPEImpl

public class PQImpl extends ANYImpl
    implements PQ
{

    public static final String RCS_ID = "$Header: PQImpl.java 1.0 2006/09/16 09:23:04 amchanda noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: PQImpl.java 1.06 2006/09/16 09:23:04 amchanda noship $", "com.sunwayhorizo.health.hl7.datatypes.common");
    private REAL quantityValue;
    private CS quantityUnit;
    private static final PQ nullNI = new PQImpl(CSImpl.valueOf("NI"));
    private static final PQ nullOTH = new PQImpl(CSImpl.valueOf("OTH"));
    private static final PQ nullNINF = new PQImpl(CSImpl.valueOf("NINF"));
    private static final PQ nullPINF = new PQImpl(CSImpl.valueOf("PINF"));
    private static final PQ nullUNK = new PQImpl(CSImpl.valueOf("UNK"));
    private static final PQ nullASKU = new PQImpl(CSImpl.valueOf("ASKU"));
    private static final PQ nullNAV = new PQImpl(CSImpl.valueOf("NAV"));
    private static final PQ nullMSK = new PQImpl(CSImpl.valueOf("MSK"));
    private static final PQ nullNA = new PQImpl(CSImpl.valueOf("NA"));
    private static final PQ nullNASK = new PQImpl(CSImpl.valueOf("NASK"));

    private PQImpl(REAL real, CS cs)
    {
        if(real == null)
            throw ANYImpl.newNullException("PQ", "constructor", "value");
        if(cs == null)
        {
            throw ANYImpl.newNullException("PQ", "constructor", "unit");
        } else
        {
            quantityValue = real;
            quantityUnit = cs;
            return;
        }
    }

    private PQImpl(CS cs)
    {
        super(true, cs);
        quantityValue = REALImpl.nullREAL(cs);
        quantityUnit = CSImpl.nullCS(cs);
    }

    public static PQ valueOf(REAL real, CS cs)
    {
        if(real.isNull().and(cs.isNull()).isTrue())
            return nullPQ(null);
        else
            return new PQImpl(real, cs);
    }

    public static PQ nullPQ()
    {
        return nullPQ(null);
    }

    public PQ toPQ(REAL real)
    {
        throw ANYImpl.newUnsupportedException("PQ", "toPQ");
    }

    public static PQ valueOf(String s, String s1)
    {
        REAL real = REALImpl.valueOf(s);
        CS cs = CSImpl.valueOf(s1);
        return new PQImpl(real, cs);
    }

    public REAL value()
    {
        return quantityValue;
    }

    public CS unit()
    {
        return quantityUnit;
    }

    public ST literal()
    {
        if(isNull().isTrue())
        {
            return STImpl.nullST(null);
        } else
        {
            StringBuffer stringbuffer = new StringBuffer(quantityValue.literal().stringValue());
            stringbuffer.append(" ");
            stringbuffer.append(quantityUnit.literal().stringValue());
            return STImpl.valueOf(stringbuffer.toString());
        }
    }

    public BL equal(ANY any)
    {
        if(any.isNull().booleanValue())
            return BLImpl.FALSE;
        if(any instanceof PQ)
        {
            if(this == (PQImpl)any)
                return BLImpl.TRUE;
            else
                return new BLImpl(value().equal(((PQ)any).value()).isTrue() && unit().equal(((PQ)any).unit()).isTrue());
        } else
        {
            return BLImpl.FALSE;
        }
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
        {
            i = 37 * i + value().hashCode();
            i = 37 * i + unit().hashCode();
        }
        return i;
    }

    public BL compares(QTY qty)
    {
        if(qty == null || isNull().or(qty.isNull()).isTrue())
            return BLImpl.FALSE;
        if(qty.dataType().equal(com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.PQ).isTrue())
        {
            PQ pq = (PQ)qty;
            if(quantityUnit.equal(pq.unit()).isTrue())
                return BLImpl.TRUE;
        }
        throw ANYImpl.newUnsupportedException("PQ", "compares");
    }

    public TYPE diffType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.PQ;
    }

    public BL lessOrEqual(PQ pq)
    {
        throw ANYImpl.newUnsupportedException("PQ", "lessOrEqual");
    }

    public BL lessOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("PQ", "lessOrEqual");
    }

    public BL greaterOrEqual(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("PQ", "greaterOrEqual");
    }

    public BL greaterThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("PQ", "greaterThan");
    }

    public BL lessThan(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("PQ", "lessThan");
    }

    public BL isZero()
    {
        return value().isZero();
    }

    public QTY minus(QTY qty)
    {
        if(compares(qty).isFalse())
        {
            return nullPQ(NullFlavorImpl.NA);
        } else
        {
            PQ pq = (PQ)qty;
            REAL real = (REAL)quantityValue.minus(pq.value());
            return new PQImpl(real, unit());
        }
    }

    public QTY plus(QTY qty)
    {
        throw ANYImpl.newUnsupportedException("PQ", "plus");
    }

    public BL compares(PQ pq)
    {
        throw ANYImpl.newUnsupportedException("PQ", "compares");
    }

    public PQ canonical()
    {
        throw ANYImpl.newUnsupportedException("PQ", "canonical");
    }

    public SET_PQR translation()
    {
        throw ANYImpl.newUnsupportedException("PQ", "translation");
    }

    public PQ negated()
    {
        throw ANYImpl.newUnsupportedException("PQ", "negated");
    }

    public PQ times(REAL real)
    {
        BigDecimal bigdecimal = value().bigDecimalValue().multiply(real.bigDecimalValue());
        return new PQImpl(REALImpl.valueOf(bigdecimal.toString()), unit());
    }

    public PQ times(PQ pq)
    {
        throw ANYImpl.newUnsupportedException("PQ", "times");
    }

    public PQ inverted()
    {
        throw ANYImpl.newUnsupportedException("PQ", "inverted");
    }

    public PQ power(INT int1)
    {
        throw ANYImpl.newUnsupportedException("PQ", "power");
    }

    public BL isOne()
    {
        return value().isOne();
    }

    public REAL originalValue()
    {
        throw ANYImpl.newUnsupportedException("PQ", "originalValue");
    }

    public CV originalUnit()
    {
        throw ANYImpl.newUnsupportedException("PQ", "originalUnit");
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.PQ;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = null;
//            if(value() != null && value().isNull().isFalse())
//            {
//                xmlelement = (XMLElement)xmldocument.createElement(s);
//                xmlelement.setAttribute("value", value().stringValue());
//            }
//            if(unit() != null && unit().isNull().isFalse())
//                xmlelement.setAttribute("unit", unit().code().stringValue());
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static PQ nullPQ(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

}
